<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑组件 -->
      <SubBread />
      <!-- 筛选区组件 -->
      <SubFilter @filter-change="filterChange" />
      <!-- 商品展示面板（排序+列表） -->
      <div class="goods-list">
        <!-- 排序组件 -->
        <SubSort @change-sort="changeSort" />
        <!-- 列表组件 -->
        <ul>
          <li v-for="i in goodsList" :key="i.id">
            <GoodsItem :goods="i" />
          </li>
        </ul>
        <XtxInfiniteLoading :loading="loading" :finished="finished" @infinite="getData" />
      </div>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
import SubBread from './components/sub-bread.vue'
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import { useRoute } from 'vue-router'
import { findSubCategoryGoods } from '@/api/category'

export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup() {
    // 1.基础布局
    // 2. 无限加载组件
    // 3. 动态加载数据且渲染
    // 4. 任何筛选条件变化需要更新列表
    // 步骤： 1.要有请求的请求体 2.有存放数据的列表  3.要有请求数据的分类id
    const loading = ref(false)
    const finished = ref(false)
    const route = useRoute()
    let dataParams = {
      page: 1,
      pagesize: 20
    }
    const goodsList = ref([])
    const getData = () => {
      // 当开始请求数据的时候
      loading.value = true
      dataParams.categoryId = route.params.id
      findSubCategoryGoods(dataParams).then(({ result }) => {
        if (result.items.length) {
          goodsList.value.push(...result.items)
          dataParams.page++
        } else {
          finished.value = true
        }
        // 请求结束
        loading.value = false
      })
    }

    // 当发生了路由切换的时候，需要重新请求当前页面的数据
    watch(
      () => route.params.id,
      (newValue) => {
        if (newValue && route.path === `/category/sub/${newValue}`) {
          // 真正切换了路由
          goodsList.value = []
          dataParams = {
            page: 1,
            pagesize: 20
          }
          finished.value = false
        }
      },
      { immediate: true }
    )

    // 1.当更改筛选组件的筛选属性，重新发送请求
    const filterChange = (filterParams) => {
      console.log(filterParams)
      finished.value = false
      dataParams = { ...dataParams, ...filterParams }
      dataParams.page = 1
      goodsList.value = []
      console.log(dataParams)
    }

    // 2.当更改排序组件的排序属性，重新发送请求

    const changeSort = (sortParams) => {
      dataParams = { ...dataParams, ...sortParams }
      dataParams.page = 1
      goodsList.value = []
      finished.value = false
    }
    console.log(dataParams)
    return { getData, loading, finished, goodsList, changeSort, filterChange }
  }
}
</script>

<style lang="less" scoped>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
